<?php

class Utility{  
    public static function getTempUploadPath($isSessionID = false)
    {
		$result = Utility::getTempPath() . '/upload';
	
		if($isSessionID)
		{
			$result .= '/' . Utility::getSessionID();
		}
		
		if (!is_dir($result)) {
			mkdir($result);
		}
		
        return $result;
    }       
	
	public static function getTempPath()
    {
		$result = realpath(Yii::app() -> basePath . '/../temp');			
		
        return $result;
    }     
	
	public static function getBasePath(){
		return realpath(Yii::app() -> basePath . '/..');
	}
	
	public static function getSessionID()
	{
		return Yii::app() -> getSession() -> getSessionID();
	}
	
	public static function getExtension($fileName, $isDot = false)
	{
		if($fileName == "") return "";
	
		$ext = preg_split("/\./", $fileName);		
		
		$result = $ext[sizeof($ext)-1];
		
		if($isDot) $result = "." . $result;
		
		return $result;
	}
		
	public static function saveImage($fileUrl = "", $fileName = "", $subFolder = "")
	{
		if($fileUrl == ""|| $fileName == "") return false;
		
		try{	
			$source = Utility::getBasePath() . "/" . $fileUrl;
			
			$destination = Utility::getBasePath() . "/images";				
							
			if($subFolder != "")
			{
				$destination .= "/" . $subFolder;
			}				
			
			$destination .=  "/" . $fileName . Utility::getExtension($fileUrl, true);
	
			if (copy($source, $destination)) {
				unlink($source);
			}
			
			return $fileName . Utility::getExtension($fileUrl, true);				
		}
		catch(Exception $ex)
		{
			return null;
		}		
	}
}
?>
